home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / dvibit.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  24KB  |  1,070 lines

  1. #ifdef __TURBOC__
  2. extern unsigned _stklen=20000;
  3. #include <alloc.h>
  4. #endif
  5. #include "all.h"
  6. #include <math.h>
  7. #include "core.h"
  8. #include "mygraph.h"
  9. #include "mydev.h"
  10. #ifndef __TURBOC__
  11. #define huge
  12. #endif
  13. #define true (!false)
  14. #define false 0
  15. int d_open(double x, double y);
  16. int dvitype(void);
  17. int draw_file(void);
  18. #define scx(v) ( ((v) * d_xscale))
  19. #define scy(v) ( ((v) * d_yscale))
  20. #define rx(v) ( ((v) * d_xscale))
  21. #define ry(v) ( ((v) * d_yscale))
  22. int gdebug=false;
  23. int nxbits,nybits;
  24. int flipit;
  25. #define dbg if (gdebug==true)
  26.  
  27. double d_scale, d_xscale, d_yscale;
  28.  
  29. int set_grey(float cur_fill);
  30. int setbitlwidth(float w);
  31. int setbitlstyle(int n);
  32. int path_bezier(float x1, float y1, float x2, float y2, float x3, float y3);
  33. int path_row(float x1, float y1, float x2, float y2);
  34. int path_box(float x1, float y1, float x2, float y2);
  35. int path_alloc(void);
  36. int path_move(float x1, float y1);
  37. int path_line(float x1, float y1);
  38. int flatten_bezier(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3);
  39. int path_closepath(void);
  40. int bitmap_print(void);
  41. int bitmap_free(void);
  42. int bitmap_alloc(void);
  43. int bitmap_paint(int row, int x1, int x2);
  44. int bitmap_pixel(int x, int y);
  45. int intline(int  x1, int  y1, int  x2, int  y2);
  46. int intbezier(float x0, float y0,float x1, float y1, float x2, float y2, float x3, float y3);
  47. #include "pathtypes.h"
  48. int hp_plus,dev_low;
  49. char outfile[80];
  50. FILE *inf,*outf;
  51. int lj;
  52. float uwidth,uheight;
  53. /*---------------------------------------------------------------------------*/
  54. char outbitname[120];
  55. FILE *outbit;
  56. main(int argc, char **argv)
  57. {
  58.     int i;
  59.     inf = fopen("out.dvi","rb");
  60.     if (inf==NULL) {
  61.         printf("Unable to open OUT.DVI for input, run DVIGLE first\n");
  62.         gle_abort("");
  63.     }
  64.  
  65.  
  66.     i = 1;
  67.     hp_plus = true;
  68.     for (i=1;i<argc; i++) {
  69.         if (strncmp(argv[i],"-o",2)==0) hp_plus = false;
  70.         else if (strncmp(argv[i],"/o",2)==0) hp_plus = false;
  71.         else if (strncmp(argv[i],"/l",2)==0) dev_low = true;
  72.         else if (strncmp(argv[i],"-l",2)==0) dev_low = true;
  73.         else if (strncmp(argv[i],"-d",2)==0) gdebug = true;
  74.         else strcpy(outbitname,argv[i]);
  75.     }
  76.     dvitype();  printf("\n");
  77. #if __TURBOC__
  78.     if (strlen(outbitname)!=0) {
  79.       printf("Writing output to file {%s} \n",outbitname);
  80.       outbit = fopen(outbitname,"wb");
  81.     } else     printf("Printing to LPT1 \n");
  82. #else
  83.             /* Special binary open for VMS */
  84.     printf("Writing output to file {%s} \n",outbitname);
  85. #ifdef unix
  86.         if ((outbit = fopen(outbitname,"wb")) == NULL)
  87. #else
  88.         if ((outbit = fopen (outbitname,"wb","rfm=fix","bls=512","mrs=512")) == NULL)
  89. #endif
  90.         {   printf ("Can't open output bitmap file.\n");
  91.             abort();
  92.         }
  93. #endif
  94.  
  95.     draw_file();
  96.     close_output();
  97. }
  98. /*---------------------------------------------------------------------------*/
  99. /*   Path variables for bitmap */
  100. typedef union {int32 l; float f;} longfloat;
  101. longfloat *path;
  102. int npath;
  103. int npath_alloc;
  104. longfloat pth[100];
  105. int npth;
  106.  
  107. int file_end = false;
  108. float getf(void);
  109. float getfx(void)
  110. {
  111.     static float x;
  112.     fread(&x,sizeof(x),1,inf);
  113.     if (feof(inf)) file_end = true;
  114.     return scx(x);
  115. }
  116. int getfxy(float *x, float *y);
  117. getfxy(float *x, float *y)
  118. {
  119.     float u,xx,yy;
  120.     xx = getf();
  121.     yy = getf();
  122.     if (flipit) {
  123.         u = yy;
  124.         yy = uwidth - xx;
  125.         xx = u;
  126.     }
  127.     *x = scx(xx);
  128.     *y = scy(yy);
  129. }
  130. float getfy(void)
  131. {
  132.     static float x;
  133.     fread(&x,sizeof(x),1,inf);
  134.     if (feof(inf)) file_end = true;
  135.     return scy(x);
  136. }
  137. float getf(void)
  138. {
  139.     static float x;
  140.     fread(&x,sizeof(x),1,inf);
  141.     if (feof(inf)) file_end = true;
  142.     return x;
  143. }
  144. int32 getl(void)
  145. {
  146.     static int32 x;
  147.     fread(&x,sizeof(x),1,inf);
  148.     if (feof(inf)) file_end = true;
  149.     return x;
  150. }
  151. float cur_lwidth,cur_color,cur_fill;
  152. int coltab[]={7,6,5,3,4,1,2,0,6,3,5,4,2};
  153. int cur_colori, cur_filli;
  154. int32 cur_lstyle;
  155. draw_file(void)
  156. {
  157.     float x,y,cx,cy,x2,y2,x3,y3,sx,sy;
  158.     int32 i;
  159.     set_grey(0.0);
  160.     for (;!file_end;) {
  161.       i = getl();
  162.       if (file_end) break;
  163.       switch (i) {
  164.         case p_size:
  165.             dbg printf("size \n");
  166.             x = getf(); y = getf();
  167.             d_open(x,y);
  168.         case p_newpath:
  169.             dbg printf("newpath \n");
  170.             npath = 0;
  171.             break;
  172.         case p_move:
  173.             getfxy(&x,&y);
  174.             cx = x; cy = y;
  175.             dbg printf("move (%d) %g %g  \n",npath,x,y);
  176.             path_move(x,y);
  177.             sx = cx; sy = cy;
  178.             break;
  179.         case p_line:
  180.             getfxy(&x,&y);
  181.             cx = x; cy = y;
  182.             dbg printf("line (%d) %g %g  \n",npath,x,y);
  183.             path_line(x,y);
  184.             break;
  185.         case p_closepath:
  186.             path_closepath();
  187.             cx = sx; cy = sy;
  188.             break;
  189.         case p_dline:
  190.             getfxy(&x,&y);
  191.             intline(cx+.5,cy+.5,x+.5,y+.5);
  192.             npath = 0;
  193.             dbg printf("dline %g %g %g %g  \n",cx,cy,x,y);
  194.             cx = x; cy = y;
  195.             break;
  196.         case p_bezier:
  197.             getfxy(&x,&y);
  198.             getfxy(&x2,&y2);
  199.             getfxy(&x3,&y3);
  200.             path_bezier(x,y,x2,y2,x3,y3);
  201.             dbg printf("bezier (%d)\n",npath);
  202.             cx = x3; cy = y3;
  203.             break;
  204.         case p_stroke:
  205.             dbg printf("stroke \n");
  206.             path_stroke();
  207.             break;
  208.         case p_fill:
  209.             dbg printf("fill \n");
  210.             path_fill();
  211.             break;
  212.         case p_lwidth:
  213.             dbg printf("lwidth \n");
  214.             cur_lwidth = getf();
  215.             setbitlwidth(cur_lwidth);
  216.             break;
  217.         case p_lstyle:
  218.             dbg printf("lstyle \n");
  219.             cur_lstyle = getl();
  220.             setbitlstyle(cur_lstyle);
  221.             break;
  222.         case p_setfilli:
  223.             cur_filli = coltab[getl()];
  224.             break;
  225.         case p_setcolori:
  226.             cur_colori = coltab[getl()];
  227.             break;
  228.         case p_setcolor:
  229.             cur_color = getf();
  230.             dbg printf("setcolor %f\n",cur_color);
  231.             break;
  232.         case p_setfill:
  233.             cur_fill = getf();
  234.             dbg printf("setfill %f\n",cur_fill);
  235.             set_grey(cur_fill);
  236.             break;
  237.         case p_null:
  238.             dbg printf("Null \n");
  239.             break;
  240.         default:
  241.             printf("Error in PATH codes in .DVI file %ld %d\n",i,p_setcolori);
  242.             break;
  243.       }
  244.     }
  245.     d_close();
  246. }
  247. gle_abort(char *s)
  248. {
  249.     exit(1);
  250. }
  251. /*--------------------------------------------------------------*/
  252. /*    Bitmap output for EPSON printers             */
  253. /*--------------------------------------------------------------*/
  254. int bitmap_print(void);
  255. char *bitmap_line(int y);
  256. int bitmap_size(int *xbits, int *ybits, double *width, double *height);
  257. /*--------------------------------------------------------------*/
  258. /*    Genric Bitmap pathed Driver 1.0 (VAX C, TURBO  C) , for GLE V3.0 */
  259. /*--------------------------------------------------------------*/
  260. /*---------------------------------------------------------------------------*/
  261. int gunit;
  262. struct gmodel g;
  263. /*---------------------------------------------------------------------------*/
  264. double xsizecm,ysizecm;
  265. #define ROW_WIDTH 19
  266. int (huge *rows)[ROW_WIDTH];
  267. #define pi 3.141592653
  268. #define true (!false)
  269. #define SOLID_LINE 1
  270. #define DASHED_LINE 2
  271. #define BLACKANDWHITE 1
  272. #define false 0
  273. int gle_debug;
  274. int getch(void);
  275. int kbhit(void);
  276. /*---------------------------------------------------------------------------*/
  277. int d_graphmode;
  278. int d_fillstyle=1,d_fillcolor;
  279. int d_lstyle,d_lwidth,d_color;
  280. int d_maxy;
  281. /*---------------------------------------------------------------------------*/
  282.  
  283. /*---------------------------------------------------------------------------*/
  284. /* -ve is used to indicate up or down */
  285. int row_min,row_max;
  286. unsigned int pcount;
  287. static int lastany = true,ffup = 3,*ffp,*lastp,ffr,ffv;
  288. static float ffx= -1,ffy= -1,lastx= -1,lasty= -1,cx= -1,cy= -1;
  289. path_pcount()
  290. {
  291.     pcount = 1;
  292. }
  293. path_reset()
  294. {
  295.     ffx = -1; ffy = -1; ffup = 3;  ffp = 0;
  296.     ffr = 0; ffv = 0;
  297.     lastany = true;
  298.     lastp = 0;
  299.     lastx = -1; lasty = -1;
  300.     cx = -1; cy = -1;
  301. }
  302. int setrowpnt(int x2, int y2,char *s);
  303. path_row(float x1, float y1, float x2, float y2)
  304. {
  305.     float dx,dy,a,b,yfix,y2fix,oldx,oldy;
  306.     int i,j,k,r,huge *m,isup=true,nofix=false,minmax=false;
  307.     int start,end;
  308.     unsigned int huge *p;
  309.     static int lastup;
  310.     float oy1,oy2,ox2,ox1;
  311.     int ee;
  312.     int enddot=false;
  313.  
  314.     dbg printf("path row %5.4g %5.4g -- %5.4g %5.4g   (%5.4g %5.4g)  (cy = %g) up=%d \n",x1,y1,x2,y2,x2-x1,y2-y1
  315.             ,cy,lastup);
  316.  
  317.     oy1 = y1; oy2 = y2; ox2 = x2; ox1 = x1;
  318.     if (ffx != -1) {
  319.         if (ffx == x2 && ffy == y2) {
  320.             enddot = true;
  321.         }
  322.     }
  323.     if (ffx == -1) { ffx = ox1; ffy = oy1; }
  324.  
  325.     if (y2==y1) goto exitrow;
  326.     if (y1>y2) {     /* downward line swap around so line is upwards */
  327.         isup = false;
  328.         dx = x1; dy = y1; x1 = x2; y1 = y2; x2 = dx; y2 = dy;
  329.     } else  {
  330.     }
  331.     dy = y2-y1; dx = x2-x1;
  332.     a = dx/dy;
  333.     b = x1 - a*y1;
  334.  
  335.     y2 = floor(y2) ;
  336.     y1 = floor(y1+1) ;
  337.  
  338.     if (enddot) if (isup) {
  339.         if (lastup) y2 = y2 - 1;
  340.     } else {
  341.         if (!lastup) y1 = y1 + 1;
  342.     }
  343.  
  344.     if (isup) {
  345.         if (lastup && cy==y1 && !lastany) y1 = y1 + 1;
  346.         start = y1; end = y2;
  347.     } else {
  348.         if (!lastup && cy==y2 && !lastany) y2 = y2 - 1;
  349.         start = y2; end = y1;
  350.     }
  351.     if (enddot && y1>y2) {
  352.         if ((isup != lastup) && !lastany) {
  353.             if (lastp!=NULL) *lastp = *lastp | (pcount << 12) ;
  354.             lastp = 0;
  355.             for (r=0;rows[ffr][r]!=0 && rows[ffr][r]!=ffv; r++) ;
  356.             ffp = &rows[ffr][r];
  357.             if (*ffp != 0) *ffp = *ffp | (pcount <<12);
  358.             pcount++;
  359.         }
  360.     }
  361.     if (y1>y2) return;
  362.     if ((isup != lastup) && !lastany) minmax = true;
  363.     if (isup) cy = y2; else cy=y1;
  364.     if (pcount==15) {printf("pcount is too big \n"); pcount=1; }
  365.  
  366.     lastup = isup;
  367.     if (ffup==3) ffup = lastup;
  368.  
  369.       if (minmax) {
  370.         if (lastp!=NULL) *lastp = *lastp |(pcount <<12);
  371.         lastp = 0;
  372.       }
  373.  
  374.  
  375.     lastany = false;
  376.     if (y1<0) y1 = 0;
  377.     if (y2>=nybits) y2 = nybits-1;
  378.     for (i = y1; i<=y2 ; i++) {
  379.       if (i<row_min) row_min = i;
  380.       if (i>row_max) row_max = i;
  381.       k = a*(i)+b+1;
  382.       for (p = &rows[i][0]; k > (0x0fff & *p) && *p!=NULL; p++) ;
  383.       for (ee = 0; rows[i][ee] != 0  && ee<=17; ee++) ;
  384.       if (ee>=17) printf("Warning, path is too complex\n");
  385.       rows[i][ee+1] = 0;
  386.       for (m = &rows[i][ee]; m >= p; m--) {
  387.         *m = *(m-1);
  388.       }
  389.       *p = k;
  390.       if (ffr == 0) if (i==start) { ffr = i; ffv=k; ffup = isup;}
  391.       if (minmax) if ((i==start)) {
  392.         *p = *p | (pcount << 12);
  393.  
  394.         pcount++;
  395.       }
  396.       if (enddot && (ffup != isup)) if (i==end) {
  397.         *p = *p | (pcount << 12);
  398.             for (r=0;rows[ffr][r]!=0 && rows[ffr][r]!=ffv; r++) ;
  399.             ffp = &rows[ffr][r];
  400.             if (*ffp != 0) *ffp = *ffp | (pcount << 12);
  401.         pcount++;
  402.       }
  403.       if (i==end) lastp = p;
  404.    dbg printf("after %d, %x %x %x %x %x %x:(%d)  pc=%x \n",i,rows[i][0],rows[i][1]
  405.         ,rows[i][2],rows[i][3],rows[i][4],rows[i][5],k,pcount);
  406.     }
  407.     exitrow:;
  408. }
  409. check()
  410. {
  411.     int i=160;
  412.     dbg printf("check %d, %d %d %d %d %d %d \n",i,rows[i][0],rows[i][1]
  413.         ,rows[i][2],rows[i][3],rows[i][4],rows[i][5]);
  414.  }
  415.  
  416. setrowpnt(int k,int i,char *s)
  417. {
  418.     int ee,*p,*m;
  419.        dbg    printf("pnt %s ",s);
  420.     if (i>=nybits || i < 0 ) return;
  421.     k+=1;
  422.     for (p = &rows[i][0]; k > *p && *p!=NULL; p++) ;
  423.     for (ee = 0; rows[i][ee] != 0  && ee<=10; ee++) ;
  424.       if (i<row_min) row_min = i;
  425.       if (i>row_max) row_max = i;
  426.     rows[i][ee+1] = 0;
  427.     for (m = &rows[i][ee]; m >= p; m--) {
  428.         *m = *(m-1);
  429.     }
  430.     *p = k;
  431.      dbg printf("xxafter %d,  %x %x %x %x %x %x    (%d)  \n",i,rows[i][0],rows[i][1]
  432.         ,rows[i][2],rows[i][3],rows[i][4],rows[i][5],k);
  433. }
  434. path_fill(void)
  435. {
  436.     float sx=0,sy=0,cx=0,cy=0,x1,y1,x2,y2,x3,y3;
  437.     int closed=true;
  438.     int i,j,p1,p2;
  439.     row_min = 20000; row_max = 0;
  440.     path_pcount();
  441.        dbg    printf("fill, npath %d \n",npath);
  442.     for (i=0;i<npath;i++) {
  443.       switch(path[i].l) {
  444.         case p_move:
  445.         if (!closed) if (cx!=sx || cy!=sy) path_row(cx,cy,sx,sy);
  446.         path_reset();
  447.         sx = path[++i].f;
  448.         sy = path[++i].f;
  449.         cx = sx; cy = sy;
  450.         break;
  451.         case p_line:
  452.         x1 = path[++i].f;
  453.         y1 = path[++i].f;
  454.         path_row(cx,cy,x1,y1);
  455.         cx = x1; cy = y1;
  456.         break;
  457.         case p_bezier:
  458.         closed = false;
  459.         x1 = path[++i].f;  y1 = path[++i].f;
  460.         x2 = path[++i].f;  y2 = path[++i].f;
  461.         x3 = path[++i].f;  y3 = path[++i].f;
  462.         flatten_bezier(cx,cy,x1,y1,x2,y2,x3,y3);
  463.         cx = x3; cy = y3;
  464.         break;
  465.         case p_closepath:
  466.         if (cx!=sx || cy!=sy) path_row(cx,cy,sx,sy);
  467.         closed = true;
  468.         cx = sx; cy = sy;
  469.         break;
  470.         default:
  471.         printf("Error in path_fill code path[%d] %ld \n",i,path[i].l);
  472.       }
  473.     }
  474.  
  475.     if (row_max >= nybits) row_max = nybits-1;
  476.     if (row_min < 0) row_min = 0;
  477.  
  478.     dbg printf("fill rows %d %d \n",row_min,row_max);
  479.     for (i=row_min; i<=row_max; i++) {
  480.       for (j=0; rows[i][j] != 0; j+=2) {
  481.         p1 = rows[i][j];
  482.         p2 = rows[i][j+1];
  483.         if ((p2 & 0xf000)!=0) if ((rows[i][j+2] & 0xf000)!=0) {
  484.           if ((p2 & 0xf000) == (rows[i][j+2] & 0xf000)) {
  485.             p2=rows[i][j+3]; j+=2;
  486.           }
  487.         }
  488.         p1 = p1 & 0x0fff; p2 = p2 & 0x0fff;
  489.         dbg printf("paint %d  (%d  %d)   %d %d\n",i,rows[i][j],rows[i][j+1],p1,p2);
  490.         bitmap_paint(i,p1 & 0x0fff,p2 & 0x0fff);
  491.         check();
  492.       }
  493.       rows[i][0] = 0; /* clear row for next fill */
  494.     }
  495.  
  496.  
  497. }
  498. path_stroke(void)
  499. {
  500.     float sx=0,sy=0,cx=0,cy=0,x1,y1,x2,y2,x3,y3;
  501.     int closed=true;
  502.     int i,j,savepath;
  503.  
  504.     savepath = g.inpath;
  505.     g.inpath = false;
  506.     for (i=0;i<npath;i++) {
  507.       switch(path[i].l) {
  508.         case p_move:
  509.         sx = path[++i].f;
  510.         sy = path[++i].f;
  511.         cx = sx; cy = sy;
  512.         break;
  513.         case p_line:
  514.         x1 = path[++i].f;
  515.         y1 = path[++i].f;
  516.         intline(cx+.5,cy+.5,x1+.5,y1+.5);
  517.         cx = x1; cy = y1;
  518.         break;
  519.         case p_bezier:
  520.         closed = false;
  521.         x1 = path[++i].f;  y1 = path[++i].f;
  522.         x2 = path[++i].f;  y2 = path[++i].f;
  523.         x3 = path[++i].f;  y3 = path[++i].f;
  524.         intbezier(cx,cy,x1,y1,x2,y2,x3,y3);
  525.         cx = x3; cy = y3;
  526.         break;
  527.         case p_closepath:
  528.         intline(cx+.5,cy+.5,sx+.5,sy+.5);
  529.         break;
  530.         default:
  531.         printf("Error in path_stroke code \n");
  532.       }
  533.     }
  534. }
  535. path_move(float x,float y)
  536. {
  537.     path_alloc();
  538.     path[npath++].l = p_move;
  539.     path[npath++].f = x;
  540.     path[npath++].f = y;
  541. }
  542. path_line(float x,float y)
  543. {
  544.     path_alloc();
  545.     path[npath++].l = p_line;
  546.     path[npath++].f = x;
  547.     path[npath++].f = y;
  548. }
  549. path_bezier(float x1, float y1, float x2, float y2,float x3, float y3)
  550. {
  551.     path_alloc();
  552.     path[npath++].l = p_bezier;
  553.     path[npath++].f = x1;    path[npath++].f = y1;
  554.     path[npath++].f = x2;    path[npath++].f = y2;
  555.     path[npath++].f = x3;    path[npath++].f = y3;
  556. }
  557. path_closepath()
  558. {
  559.     path_alloc();
  560.     path[npath++].l = p_closepath;
  561. }
  562. path_box(float x1, float y1, float x2, float y2)
  563. {
  564.     path_move(x1,y1);
  565.     path_line(x2,y1);
  566.     path_line(x2,y2);
  567.     path_line(x1,y2);
  568.     path_closepath();
  569. }
  570. path_alloc(void)
  571. {
  572.     static int npa;
  573.     int32 *a;
  574.     if (npath < (npath_alloc-20)) return;
  575.     npath_alloc = 20 + 2 * npath;
  576.     a = calloc(1,npath_alloc*sizeof(int32));
  577.     if (a==NULL) {
  578.         gle_abort("Unable to allocate memory for path \n");
  579.     }
  580.     dbg printf("path allocate %d \n",npa);
  581.     if (path != NULL) {
  582.         dbg printf("path reallocate, copy \n");
  583.         memcpy(a,path,(npa)*sizeof(int32));
  584.         free(path);
  585.     }
  586.     npa = npath_alloc;
  587.     path = a;
  588. }
  589. path_free(void)
  590. {
  591.     if (path==NULL) return;
  592.     free(path);
  593.     path = NULL;
  594.     npath = 0;
  595.     npath_alloc = 0;
  596. }
  597. /*---------------------------------------------------------------------------*/
  598.  
  599. d_devcmd(char *s)
  600. {}
  601. /*---------------------------------------------------------------------------*/
  602. d_dfont(char *c)
  603. {
  604.     /* only used for the DFONT driver which builds fonts (never used)*/
  605. }
  606. /*---------------------------------------------------------------------------*/
  607. d_message(char *s)
  608. {
  609.     printf("%s\n",s);
  610. }
  611. /*---------------------------------------------------------------------------*/
  612. d_source(char *s)
  613. {
  614.     s=s;
  615. }
  616. /*---------------------------------------------------------------------------*/
  617. d_get_type(char *t)
  618. {
  619.     strcpy(t,"OUTPUT, BITMAP, BLACKANDWHITE, HARDCOPY");
  620. }
  621. /*---------------------------------------------------------------------------*/
  622. d_set_path(int onoff)
  623. {}
  624. /*---------------------------------------------------------------------------*/
  625. d_newpath()
  626. {
  627.     npath = 0;
  628. }
  629. /*---------------------------------------------------------------------------*/
  630. int fittobit; /* modified in DVIWP */
  631. int bitmap_size(int *xpix, int *ypix, double *width, double *height);
  632. d_open(double width, double height)
  633. {
  634.     double f,f1,f2,fx,fy;
  635.     /* nxbits, nybits, xsizecm, ysizecm */
  636.     /* Get largest rectangle we can fit on the screen */
  637.  
  638.     bitmap_size(&nxbits, &nybits, &xsizecm, &ysizecm);
  639. #if __TURBOC__
  640.     rows = farcalloc(sizeof(int)*ROW_WIDTH,nybits+30);
  641. #else
  642.     rows = calloc(sizeof(int)*ROW_WIDTH,nybits+30);
  643. #endif
  644.     if (rows==0) printf("Unable to allocate rows %d %d\n",
  645.         sizeof(int)*ROW_WIDTH,nybits+30);
  646.     dbg printf("allocated rows %d %d\n",
  647.         sizeof(int)*ROW_WIDTH,nybits+30);
  648.     dbg printf("Allcate rows %ld\n",sizeof(int)*(int32) nybits*ROW_WIDTH+30);
  649.     showfree();
  650.     if (rows==NULL) gle_abort("Unable to allocate row array\n");
  651.     uwidth = width; uheight = height;
  652.  
  653.  
  654.     d_scale = xsizecm / width;
  655.     f = ysizecm / height;
  656.     if (f<d_scale) d_scale = f;
  657.  
  658.     if (fittobit) {
  659.         fx = xsizecm / width;
  660.         d_scale = 1;
  661.         fy = ysizecm / height;
  662.     }
  663.     if (d_scale<.98) {
  664.         if (width>height) {
  665.             d_scale = ysizecm/width;
  666.             f = xsizecm/height;
  667.             if (f<d_scale) d_scale = f;
  668.             flipit = true;
  669.             printf("Flipping graph to fit on page better \n");
  670.         }
  671.     }
  672.  
  673.     bitmap_alloc();
  674.  
  675.     /* Get correct size of drawing */
  676.  
  677.     if (d_scale > 1.001) d_scale = 1;
  678.     else {
  679.         if (d_scale < .99) printf("Squashing onto page by factor %g \n",d_scale);
  680.     }
  681.  
  682.     d_xscale = d_scale * (nxbits-2) / xsizecm; /* Device Scale X, Device Scale y */
  683.     d_yscale = d_scale * (nybits-2) / ysizecm;
  684.     if (fittobit) {
  685.         d_xscale = fx * (nxbits-2) / xsizecm;
  686.         d_yscale = fy * (nybits-2) / ysizecm;
  687.     }
  688. }
  689. /*---------------------------------------------------------------------------*/
  690. d_tidyup()
  691. {
  692.     printf("AARRRrrrrgg,  Dieing\n");
  693. }
  694. showfree()
  695. {
  696. #ifdef __TURBOC__
  697.     struct heapinfo hi;
  698.     hi.ptr = NULL;
  699.     dbg printf("Heapcheck %d ",heapcheck());
  700. /*    printf("Core memory left %ld \n",coreleft()); */
  701.     dbg while (heapwalk(&hi)== _HEAPOK)
  702.         printf("%ld   %s\n",hi.size,hi.in_use ? "used" : "free");
  703. #endif
  704. }
  705. d_close()
  706. {
  707.     d_flush();
  708.     showfree();
  709.     if (rows!=NULL) free(rows);
  710.     path_free();
  711.     showfree();
  712.     bitmap_print();
  713.     bitmap_free();
  714. }
  715. /*---------------------------------------------------------------------------*/
  716. d_set_line_cap(int i)
  717. {
  718.     i++;
  719. }
  720. /*---------------------------------------------------------------------------*/
  721. d_set_line_join(int i)
  722. {
  723.     i++;
  724. }
  725. /*---------------------------------------------------------------------------*/
  726. d_set_line_miterlimit(double d)
  727. {
  728.     int i=0;
  729.     i++;
  730. }
  731. /*---------------------------------------------------------------------------*/
  732. d_set_line_width(double w)
  733. {
  734.     d_lwidth = 1;
  735.     if (w>.099) d_lwidth = 3;
  736. }
  737. /*---------------------------------------------------------------------------*/
  738. d_set_line_styled(double dd)
  739. {}
  740. d_set_line_style(char *s)
  741. {
  742.     d_lstyle = DASHED_LINE;
  743.     if (strcmp(s,"")==0) d_lstyle = SOLID_LINE;
  744.     if (strcmp(s,"1")==0) d_lstyle = SOLID_LINE;
  745. }
  746. /*---------------------------------------------------------------------------*/
  747. d_fill()
  748. {
  749.     path_fill();
  750. }
  751. /*---------------------------------------------------------------------------*/
  752. d_fill_ary(int nwk,double (*wkx)[],double (*wky)[])
  753. {
  754.     int i;
  755.  
  756.     path_move( (*wkx)[0], (*wky)[0]);
  757.     for (i=1;i<nwk;i++) {
  758.         path_line( (*wkx)[i], (*wky)[i]);
  759.     }
  760.     d_fill();
  761. }
  762. d_line_ary(int nwk,double (*wkx)[],double (*wky)[])
  763. {
  764.     int i;
  765.  
  766.     path_move( (*wkx)[0], (*wky)[0]);
  767.     for (i=1;i<nwk;i++) {
  768.         path_line( (*wkx)[i], (*wky)[i]);
  769.     }
  770.     d_stroke();
  771. }
  772. /*---------------------------------------------------------------------------*/
  773. d_stroke()
  774. {
  775.     path_stroke();
  776. }
  777. /*---------------------------------------------------------------------------*/
  778. d_clip()
  779. {
  780. }
  781. /*---------------------------------------------------------------------------*/
  782. d_set_matrix(double newmat[3][3])
  783. {
  784.  
  785. }
  786. /*---------------------------------------------------------------------------*/
  787. d_move(double zx,double zy)
  788. {
  789. }
  790. /*---------------------------------------------------------------------------*/
  791. d_reverse()     /* reverse the order of stuff in the current path */
  792. {
  793. }
  794. /*---------------------------------------------------------------------------*/
  795. d_closepath()
  796. {
  797.     path_closepath();
  798. }
  799. int bit_line( double x, double y);
  800. /*---------------------------------------------------------------------------*/
  801. intbezier(float x0, float y0,float x1, float y1, float x2, float y2, float x3, float y3)
  802. {
  803.     float ax,bx,cx,ay,by,cy,dist,xx,yy;
  804.     float xxx,yyy,i,t,nstep;
  805.     dist = fabs(x3-x0) + fabs(y3-y0);
  806.     nstep = 10;
  807.     if (dist>3) nstep = 20;
  808.     if (dist<.5) nstep = 5;
  809.     if (dist<.3) nstep = 3;
  810.      if (dist<.1) {
  811.         intline(x0+.5,y0+.5,x3+.5,y3+.5);
  812.         return;
  813.     }
  814.     cx = (x1-x0)*3;
  815.     bx = (x2-x1)*3-cx;
  816.     ax = x3-x0-cx-bx;
  817.     cy = (y1-y0)*3;
  818.     by = (y2-y1)*3-cy;
  819.     ay = y3-y0-cy-by;
  820.     xx = x0; yy = y0;
  821.     for (i=0;i<=nstep;i++) {
  822.         t = i/nstep;
  823.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  824.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  825.         intline(xx+.5,yy+.5,xxx+.5,yyy+.5);
  826.         xx = xxx; yy = yyy;
  827.     }
  828. }
  829.  
  830. uint32 PATTERN = 0xFFFFFFFF;
  831. int LINEWIDTH=1;
  832. setbitlstyle(int n)
  833. {
  834.     uint32 style_type[9] = {0xffffffff,
  835.         0xffffffff, 0xc0c0c0c0, 0xff00ff00, 0xfff0fff0
  836.         ,0xffff0000, 0xfffff0f0, 0xf00000f0, 0xf0f0f0f0};
  837.     if (n>8) n = 8;
  838.     PATTERN = style_type[n];
  839. }
  840. setbitlwidth(float w)
  841. {
  842.     if (w==0) LINEWIDTH=1;
  843.     else {
  844.         LINEWIDTH = scx(w);
  845.     }
  846.     if (LINEWIDTH<=1) LINEWIDTH=1;
  847. }
  848. intline(int  x1, int  y1, int  x2, int  y2)
  849. {
  850. #define sign(x) ((x) > 0 ? 1:  ((x) == 0 ? 0:  (-1)))
  851.     int dx, dy, dxabs, dyabs, i, j, px, py, sdx, sdy, x, y;
  852.     static uint32 mask=0x80000000;
  853.  
  854.     if (x1<0) x1 = 0;
  855.     if (x1>nxbits) x1 = nxbits-1;
  856.     if (y1<0) y1 = 0;
  857.     if (y1>nybits) y1 = nybits-1;
  858.     if (x2<0) x2 = 0;
  859.     if (x2>nxbits) x2 = nxbits-1;
  860.     if (y2<0) y2 = 0;
  861.     if (y2>nybits) y2 = nybits-1;
  862.  
  863.     /* line algorithym begins */
  864.  
  865.  
  866.     dx = x2 - x1;
  867.     dy = y2 - y1;
  868.     sdx = sign(dx);
  869.     sdy = sign(dy);
  870.     dxabs = abs(dx);
  871.     dyabs = abs(dy);
  872.     x = 0;
  873.     y = 0;
  874.     px = x1;
  875.     py = y1;
  876.  
  877.  
  878.  
  879.     if (dxabs >= dyabs)
  880.     {
  881.  
  882.             mask = mask ? mask : 0x80000000;
  883.             if (PATTERN & mask)
  884.                 {
  885.                 for (j= -LINEWIDTH/2; j<=LINEWIDTH/2; j++)
  886.                     bitmap_pixel(px,py+j);
  887.             }
  888.             mask >>= 1;
  889.  
  890.  
  891.  
  892.         for (i=0; i<dxabs; i++)
  893.         {
  894.             mask = mask ? mask : 0x80000000;
  895.             y += dyabs;
  896.             if (y>=dxabs)
  897.             {
  898.                 y -= dxabs;
  899.                 py += sdy;
  900.             }
  901.             px += sdx;
  902.             if (PATTERN & mask)
  903.                 {
  904.                 for (j= -LINEWIDTH/2; j<=LINEWIDTH/2; j++)
  905.                     bitmap_pixel(px,py+j);
  906.             }
  907.             mask >>= 1;
  908.         }
  909.     }
  910.     else
  911.     {
  912.             mask = mask ? mask : 0x80000000;
  913.             if (PATTERN & mask)
  914.                 {
  915.                 for (j= -LINEWIDTH/2; j<=LINEWIDTH/2; j++)
  916.                     bitmap_pixel(px+j,py);
  917.             }
  918.             mask >>= 1;
  919.  
  920.         for (i=0; i<dyabs; i++)
  921.         {
  922.             mask = mask ? mask : 0x80000000;
  923.             x += dxabs;
  924.             if (x>=dyabs)
  925.             {
  926.                 x -= dyabs;
  927.                 px += sdx;
  928.                 }
  929.             py += sdy;
  930.             if (PATTERN & mask)
  931.             {
  932.                 for (j= -LINEWIDTH/2; j<=LINEWIDTH/2; j++)
  933.                     bitmap_pixel(px+j,py);
  934.             }
  935.                 mask >>= 1;
  936.         }
  937.     }
  938. }
  939.  
  940. /*---------------------------------------------------------------------------*/
  941. d_clear()
  942. {
  943.     d_lstyle = SOLID_LINE;
  944.     d_lwidth = 1;
  945.  
  946. }
  947. /*---------------------------------------------------------------------------*/
  948. d_flush()
  949. {
  950. }
  951. flatten_bezier(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3)
  952. {
  953.     float  ax,bx,cx,ay,by,cy,dist;
  954.     float xxx,yyy,i,t,nstep,xx,yy;
  955.     dist = fabs(x3-x0) + fabs(y3-y0);
  956.     nstep = 12;
  957.     if (dist<1) nstep = 7;
  958.     if (dist<.5) nstep = 3;
  959.     if (dist<.1) {
  960.         path_row(x0,y0,x3,y3);
  961.         return;
  962.     }
  963.     cx = (x1-x0)*3;
  964.     bx = (x2-x1)*3-cx;
  965.     ax = x3-x0-cx-bx;
  966.     cy = (y1-y0)*3;
  967.     by = (y2-y1)*3-cy;
  968.     ay = y3-y0-cy-by;
  969.     xx = x0;
  970.     yy = y0;
  971.     for (i=0;i<=nstep;i++) {
  972.         t = i/nstep;
  973.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  974.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  975.         path_row(xx,yy,xxx,yyy);
  976.         xx = xxx; yy = yyy;
  977.     }
  978. }
  979. d_beginclip()
  980. {
  981. }
  982. d_endclip()
  983. {
  984. }
  985.  
  986.  
  987. #ifdef __TURBOC__
  988. #include <dos.h>
  989. char put_out(char c);
  990. int status(void);
  991. status(void)
  992. {
  993.     union REGS reg;
  994.  
  995.     reg.h.ah = 2;
  996.     reg.x.dx = 0;
  997.     int86(0x17, ®, ®);
  998.     return (reg.h.ah & 0x80);
  999. }
  1000. char put_out(char c)
  1001. {
  1002.     union REGS reg;
  1003.     int w=0;
  1004.     int ft=0;
  1005.  
  1006.     if (outbit!=NULL) {
  1007.         fputc(c,outbit);
  1008.         return;
  1009.     }
  1010.     while (!status()) {
  1011.         w++;
  1012.         if (w>10000) if (ft==0) {printf("Printer Busy "); w=0; ft=1;}
  1013.         if (w>10000) {printf(".");  w=0;}
  1014.     }
  1015.     if (ft==1) printf("\n");
  1016.     reg.h.ah = 0;
  1017.     reg.h.al = c;
  1018.     reg.x.dx = 0;
  1019.     int86(0x17,®,®);
  1020.     return (reg.h.ah);
  1021. }
  1022. close_output()
  1023. {
  1024.     if (outbit!=NULL) fclose(outbit);
  1025. }
  1026.  
  1027. #else
  1028.  
  1029. #include <stdio.h>
  1030. #ifdef VAXC
  1031. #include <file.h>
  1032. #endif
  1033. char zzbuff[512];
  1034. int nzz=0;
  1035. put_out(char c)
  1036. {
  1037.     zzbuff[nzz++] = c;
  1038.     if (nzz==512) {
  1039.         fwrite(zzbuff,nzz,1,outbit);
  1040.         nzz = 0;
  1041.     }
  1042. }
  1043. close_output()
  1044. {
  1045.     if (nzz > 0)  fwrite(zzbuff,nzz,1,outbit);
  1046.     fclose(outbit);
  1047. }
  1048.  
  1049. #endif
  1050.  
  1051. #ifndef unix
  1052. int printmem(char *s, int n);
  1053. void pprintf(va_list arg_list, ...)
  1054. {
  1055.      va_list arg_ptr;
  1056.      char *format;
  1057.     char output[200];
  1058.  
  1059.      va_start(arg_ptr, arg_list);
  1060.      format = arg_list;
  1061.      vsprintf(output, format, arg_ptr);
  1062.     printmem(output,strlen(output));
  1063. }
  1064. #endif
  1065. printmem(char *s, int n)
  1066. {
  1067.     for (;n>0;n--) put_out(*s++);
  1068. }
  1069.  
  1070.